import org.xml.sax.*;
import java.util.*;
import java.io.*;

public class XMLContentHandler implements ContentHandler {
	private boolean entry_open = false;
	private String current_node = "";
	private Hashtable settings;
	private String localmatch = "";
	private	String localuri = "";
	private	String searchnode = "";
	private	String searchnamespace = "";
	private String iCal_timestamp = "";
	private String temp_dir = "";
		
	XMLContentHandler(Hashtable insettings) {
		settings = insettings;
		
		String prefix = (String)settings.get("metadataPrefix");
		iCal_timestamp = (String)settings.get("iCal_timestamp");
                
		temp_dir = (String)settings.get("temp_dir");
		if (temp_dir.substring(temp_dir.length()-1,temp_dir.length()).equals("/")){} else { temp_dir += "/"; }
		
		if (prefix.equals("rem_atom")) {
			localmatch="entry"; localuri="http://www.w3.org/2005/Atom"; searchnode="id"; searchnamespace="http://www.w3.org/2005/Atom";
		} else
		if (prefix.equals("mets")) {
			localmatch="fileSec"; localuri="http://www.loc.gov/METS/"; searchnode="FLocat"; searchnamespace="http://www.loc.gov/METS/";
		} else
		if (prefix.equals("oai_dc")) {
			localmatch="identifier"; localuri="http://purl.org/dc/elements/1.1/"; searchnode="identifier"; searchnamespace="http://purl.org/dc/elements/1.1/";
		} else
		if (prefix.equals("didl")) {
			localmatch="Component"; localuri="urn:mpeg:mpeg21:2002:02-DIDL-NS"; searchnode="Resource"; searchnamespace="urn:mpeg:mpeg21:2002:02-DIDL-NS";
		} else {
			System.err.println("Invalid or unrecognised input format");
			System.exit(0);
		}
	}

	public void setDocumentLocator (Locator locator) { 
	}

	public void startDocument () throws SAXException  { 
	}

	public void endDocument() throws SAXException  { 
	}

	public void startPrefixMapping (String prefix, String uri) throws SAXException  {
	}

	public void endPrefixMapping (String prefix) throws SAXException  { 
	}

	public void startElement (String uri, String localName, String qName, Attributes atts) throws SAXException  { 
		//System.out.println("uri: " + uri);
		//System.out.println("localName: " + localName);
		//System.out.println("qName: " + qName);
		if (localName.equals(localmatch) && uri.equals(localuri)) {
			entry_open=true;
		}
		if (entry_open && localName.equals(searchnode) && uri.equals(searchnamespace)) {
			current_node = searchnode;
		}
		String prefix = (String)settings.get("metadataPrefix");
		if (prefix.equals("mets") && (current_node.equals(searchnode))) {
			writeDroidList(atts.getValue("xlink:href"));
		}
		if (prefix.equals("didl") && (current_node.equals(searchnode))) {
			writeDroidList(atts.getValue("ref"));
		}
	}

	public void endElement (String uri, String localName, String qName) throws SAXException { 
		if (localName.equals(localmatch) && uri.equals(localuri)) {
			entry_open=false;
		}
		current_node = "";
	}

	public void characters (char ch[], int start, int length) throws SAXException { 
		String handle = "";
		if (current_node.equals(searchnode)) {
			for (int i=start;i<(start+length);i++) {
				handle = handle + ch[i];
			}
			/**
			  * Yuk, FIXME, This code should be removed really
			  */
			if (handle.length() > 4 && (handle.substring(0,4)).equals("http")) {
				if (handle.indexOf("http://oreproxy.org/r?what=") > -1) {
					handle = handle.replace("http://oreproxy.org/r?what=","");
				} 
				handle.trim();
				writeDroidList(handle);
			}
		}
	}

	public void ignorableWhitespace (char ch[], int start, int length) throws SAXException { 
	}

	public void processingInstruction (String target, String data) throws SAXException { 
	}

	public void skippedEntity (String name) throws SAXException { 
	}
	
	public void writeDroidList(String uri) {
		try {
			BufferedWriter out = new BufferedWriter(new FileWriter(temp_dir+"droid_"+iCal_timestamp+".xml",true));
			out.write("\t<IdentificationFile Name=\""+uri+"\" />");
			out.newLine();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}		
	}	

}

